package com.allwees.bs.core.model.user.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.allwees.bs.core.model.constant.EPromotionChannel;
import com.allwees.bs.core.model.user.constant.EInfluencerType;
import com.allwees.bs.core.model.user.constant.EUserSourceType;
import com.allwees.bs.core.modelbase.entity.BaseEntity;
import com.allwees.core.common.constant.EStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import java.util.Date;

/**
 * 用户
 *
 * @author yangylsky
 * @since 2019-08-21 09:26
 *
 */
@Data
@EqualsAndHashCode(callSuper = true)
@MappedSuperclass
public abstract class UserModel extends BaseEntity {
    private static final long serialVersionUID = 2576124755721881744L;
    private String firstName;

    private String lastName;

    private String email;

    @JsonIgnore
    @JSONField(serialize = false)
    private String password;

    private String inviterUuid;

    private String shareCode;

    @Enumerated(EnumType.STRING)
    protected EPromotionChannel inviteChannel;

    @Enumerated(EnumType.STRING)
    private EUserSourceType sourceType;

    private Integer birthdayYear;

    private Integer birthdayMonth;

    private Integer birthdayDay;

    private String avatar;

    private String itu;

    private String phone;

    private String language;

    private Integer country;

    private Integer city;

    private String address;

    private String zipCode;

    private String currency;

    /**
     * 用户性别
     *
     * 1:男
     * 2:女
     * 3:未知
     */
    private Integer gender;

    private String facebookId;

    private String twitterId;

    private String googleId;

    private String appleId;

    private String aboutMe;

    private Integer identityVerify = 0;

    private Integer emailVerified = 0;

    private Integer phoneVerified = 0;

    private Integer online = 0;

    private Integer locked = 0;

    private Date latestLoginAt;

    private String deviceSource;

    private String deviceId;

    private String deviceModel;

    private String systemVersion;

    private String market;

    private String bundleId;

    private String appName;

    private String appVersion;

    private Float longitude;

    private Float latitude;

    // 用户身份，0正常用户，-1内部用户，-2马甲用户
    @Column(name = "role")
    private Integer role = 0;

    /**
     * 注册时使用的硬件平台(ios/android等) 用于统计
     */
    private String agent;

    /**
     * 是否产生过订单
     */
    private Date firstPaymentAt;

    @Transient
    @TableField(exist = false)
    private String inviterShareCode;

    /** 推广码 **/
    private String promoCode;

    @Column(length = 64)
    private String icsUuid;

    @Column
    private Integer isInfluencer;

    @Column
    private String belongName;

    @Transient
    public boolean isDisabled(){
        return EStatus.DISABLED.getValue().equals(status);
    }

    public String getIsInfluencerDesc() {
        return isInfluencer != null ? EInfluencerType.getEInfluencerType(isInfluencer).desc() : "否";
    }

    public  Integer getIsInfluencer() {
        if(this.isInfluencer == null ){
            return 0;
        }
        return this.isInfluencer;
    }

    @Transient
    public boolean isHasOrder(){
        return firstPaymentAt != null;
    }

    public String thirdId(){
        if (StringUtils.isNotBlank(appleId)) {
            return appleId;
        }
        if (StringUtils.isNotBlank(facebookId)) {
            return facebookId;
        }
        if (StringUtils.isNotBlank(googleId)) {
            return googleId;
        }
        return null;
    }
}

